package org.lld.im.service.message.controller;

import org.lld.im.common.ResponseVO;
import org.lld.im.common.enums.command.MessageCommand;
import org.lld.im.common.model.SyncReq;
import org.lld.im.common.model.message.CheckSendMessageReq;
import org.lld.im.service.group.service.GroupMessageService;
import org.lld.im.service.message.model.req.SendMessageReq;
import org.lld.im.service.message.service.MessageSyncService;
import org.lld.im.service.message.service.P2PMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: LunBoWang
 * @description: TODO
 * @date: 2025/4/4 下午8:18
 * @version: 1.0
 */
@RestController
@RequestMapping("v1/message")
public class MessageController {
    @Resource
    P2PMessageService p2PMessageService;
    @Resource
    private GroupMessageService groupMessageService;
    @Resource
    MessageSyncService messageSyncService;


    @RequestMapping("/send")
    public ResponseVO send(@RequestBody @Validated SendMessageReq req, Integer appId) {
        req.setAppId(appId);
        return ResponseVO.successResponse(p2PMessageService.send(req));
    }

    @RequestMapping("/checkSend")
    public ResponseVO checkSend(@RequestBody @Validated CheckSendMessageReq req) {
        if (req.getCommand() == MessageCommand.MSG_P2P.getCommand()) {
            return p2PMessageService.imServerPermissionCheck(req.getFromId(), req.getToId(), req.getAppId());
        } else {
            return groupMessageService.imServerPermissionCheck(req.getFromId(), req.getToId(), req.getAppId());
        }
    }

    @RequestMapping("/syncOfflineMessage")
    public ResponseVO syncOfflineMessage(@RequestBody @Validated SyncReq req, Integer appId) {
        req.setAppId(appId);
        return messageSyncService.syncOfflineMessage(req);
    }
}
